package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.Organization;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.RecordCourse;
import com.xmy.cultivate.entity.StudentGrade;
import com.xmy.cultivate.entity.views.StatementData;
import com.xmy.cultivate.mapper.*;
import com.xmy.cultivate.service.IFinanceService;
import com.xmy.cultivate.service.IOrganizationService;
import com.xmy.cultivate.service.IStudentGradeService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.CommonUtil;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;

@Service
public class FinanceServiceImpl implements IFinanceService {

    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Autowired
    RecordCourseMapper recordCourseMapper;

    @Autowired
    IOrganizationService organizationService;

    @Autowired
    IStudentGradeService iStudentGradeService;

    @Override
    public IPage<StatementData> statementData(IPage<?> page,String schoolId, String startDate,String endDate, String subjectsId,String studentName,Quarter quarter,Map reMap) {

        if(StringUtils.isBlank(startDate) && StringUtils.isBlank(endDate)){
            startDate = quarter.getStartDate().toString();
            endDate = quarter.getEndDate().toString();
        }

        if(StringUtils.isBlank(startDate)){
            startDate = quarter.getStartDate().toString();
        }
        if(StringUtils.isBlank(endDate)){
            endDate = quarter.getEndDate().toString();
        }

        Integer yearAndQuarter = 0;
        if(quarter != null){
            //throw new CommonException(ResultCode.quarterNo);
            yearAndQuarter = Integer.parseInt(quarter.getYearPart()+""+quarter.getNum());
        }


        IPage<StatementData> statementDataIPage =  orderDetailMapper.statementData(page,schoolId,subjectsId,yearAndQuarter,studentName);

        String schoolIdString = "";
        for (StatementData statementData:statementDataIPage.getRecords()){

            if (statementData.getYearClassId() != null){
                statementData.setYearClassId(statementData.getYearClassId());
                statementData.setYearClassName(CommonUtil.getYearClassNameForId(statementData.getYearClassId()));
            }

            //实际使用课时，与金额
            QueryWrapper<RecordCourse> recordCourseQueryWrapper = new QueryWrapper<>();
            recordCourseQueryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
            recordCourseQueryWrapper.eq("year_part",quarter.getYearPart());
            recordCourseQueryWrapper.eq("quarter",quarter.getNum());
            recordCourseQueryWrapper.eq(StringUtils.isNotBlank(subjectsId),"subjects_id",subjectsId);
            recordCourseQueryWrapper.between("school_date",startDate,endDate);
            recordCourseQueryWrapper.eq("is_deduct_house",1);
            recordCourseQueryWrapper.eq("is_deduct_money",1);
            recordCourseQueryWrapper.eq("student_id",statementData.getStudentId());
            recordCourseQueryWrapper.eq("deleted",0);

            statementData.setAdvancePrice(statementData.getResiduePrice());



            /**
             * 请假课次
             */
            recordCourseQueryWrapper = new QueryWrapper<>();
            recordCourseQueryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
            recordCourseQueryWrapper.eq("year_part",quarter.getYearPart());
            recordCourseQueryWrapper.eq("quarter",quarter.getNum());
            recordCourseQueryWrapper.eq(StringUtils.isNotBlank(subjectsId),"subjects_id",subjectsId);
            recordCourseQueryWrapper.between("school_date",startDate,endDate);
            recordCourseQueryWrapper.eq("is_deduct_house",1);
            recordCourseQueryWrapper.ne("is_repair",1);//不等于已补课的
            recordCourseQueryWrapper.ne("status",1);//不等于1
            recordCourseQueryWrapper.eq("student_id",statementData.getStudentId());
            recordCourseQueryWrapper.eq("deleted",0);
            Integer leaveCourseCount = recordCourseMapper.getLeaveCourseCount(recordCourseQueryWrapper);
            statementData.setLeaveCourseCount(leaveCourseCount);

            //校区名称
            Organization byId = organizationService.getById(statementData.getSchoolId());
            if (byId != null) {
                statementData.setSchoolName(byId.getName());
            }

            if (schoolIdString.equals("")) {
                schoolIdString = statementData.getSchoolId().toString();
            } else {
                schoolIdString = schoolIdString + "," + statementData.getSchoolId().toString();
            }

        }

        reMap.put("schoolId",schoolIdString);
        //在读学员
        List<StudentGrade> inReadStudentList = iStudentGradeService.inReadStudentList(reMap);
//        System.out.println("inReadStudentList:"+inReadStudentList);
        //异常学员
        List<StudentGrade> errorStudentList = iStudentGradeService.abnormalStudentList(reMap);
//        System.out.println("errorStudentList:"+errorStudentList);
        //停课学员
        List<StudentGrade> stopStudentList = iStudentGradeService.stopStudentList(reMap);
//        System.out.println("stopStudentList:"+stopStudentList);
        //退费学员
        List<StudentGrade> returnStudentList = iStudentGradeService.returnStudentList(reMap);
//        System.out.println("returnStudentList:"+returnStudentList);
        //结课学员
        List<StudentGrade> overStudentList = iStudentGradeService.overStudentList(reMap);
//        System.out.println("overStudentList:"+overStudentList);
        //转校学员
        List<StudentGrade> changeStudentList = iStudentGradeService.changeStudentList(reMap);
//        System.out.println("changeStudentList:"+changeStudentList);
        for (StatementData statementData:statementDataIPage.getRecords()){
            statementData.setInReadStudentCount(0);
            statementData.setErrorStudentCount(0);
            statementData.setStopStudentCount(0);
            statementData.setReturnStudentCount(0);
            statementData.setOverStudentCount(0);
            statementData.setChangeStudentCount(0);
            for (StudentGrade val:inReadStudentList) {
                if (Objects.equals(statementData.getSchoolId(), val.getStudentId())) {
                    statementData.setInReadStudentCount(Integer.valueOf(val.getYearClassId().toString()));
                }
            }
            for (StudentGrade val:errorStudentList) {
                if (Objects.equals(statementData.getSchoolId(), val.getStudentId())) {
                    statementData.setErrorStudentCount(Integer.valueOf(val.getYearClassId().toString()));
                }
            }
            for (StudentGrade val:stopStudentList) {
                if (Objects.equals(statementData.getSchoolId(), val.getStudentId())) {
                    statementData.setStopStudentCount(Integer.valueOf(val.getYearClassId().toString()));
                }
            }
            for (StudentGrade val:returnStudentList) {
                if (Objects.equals(statementData.getSchoolId(), val.getStudentId())) {
                    statementData.setReturnStudentCount(Integer.valueOf(val.getYearClassId().toString()));
                }
            }
            for (StudentGrade val:overStudentList) {
                if (Objects.equals(statementData.getSchoolId(), val.getStudentId())) {
                    statementData.setOverStudentCount(Integer.valueOf(val.getYearClassId().toString()));
                }
            }
            for (StudentGrade val:changeStudentList) {
                if (Objects.equals(statementData.getSchoolId(), val.getStudentId())) {
                    statementData.setChangeStudentCount(Integer.valueOf(val.getYearClassId().toString()));
                }
            }
        }
        //排序
//        在读学员studentSort 1异常学员studentSort 2停课学员studentSort 3退费学员studentSort 4结课学员studentSort 5转校学员studentSort 6
        if (reMap.get("studentSort") != null) {
            String studentSort = reMap.get("studentSort").toString();
            List<StatementData> objects = new ArrayList<>();
            if (Objects.equals(studentSort, "1")) {
                for (StudentGrade val: inReadStudentList) {
                    for (StatementData data: statementDataIPage.getRecords()) {
                        if (Objects.equals(val.getStudentId(), data.getSchoolId())) {
                            objects.add(data);
                        }
                    }
                }
            }
            if (Objects.equals(studentSort, "2")) {
                for (StudentGrade val: errorStudentList) {
                    for (StatementData data: statementDataIPage.getRecords()) {
                        if (Objects.equals(val.getStudentId(), data.getSchoolId())) {
                            objects.add(data);
                        }
                    }
                }
            }
            if (Objects.equals(studentSort, "3")) {
                for (StudentGrade val: stopStudentList) {
                    for (StatementData data: statementDataIPage.getRecords()) {
                        if (Objects.equals(val.getStudentId(), data.getSchoolId())) {
                            objects.add(data);
                        }
                    }
                }
            }
            if (Objects.equals(studentSort, "4")) {
                for (StudentGrade val: returnStudentList) {
                    for (StatementData data: statementDataIPage.getRecords()) {
                        if (Objects.equals(val.getStudentId(), data.getSchoolId())) {
                            objects.add(data);
                        }
                    }
                }
            }
            if (Objects.equals(studentSort, "5")) {
                for (StudentGrade val: overStudentList) {
                    for (StatementData data: statementDataIPage.getRecords()) {
                        if (Objects.equals(val.getStudentId(), data.getSchoolId())) {
                            objects.add(data);
                        }
                    }
                }
            }
            if (Objects.equals(studentSort, "6")) {
                for (StudentGrade val: changeStudentList) {
                    for (StatementData data: statementDataIPage.getRecords()) {
                        if (Objects.equals(val.getStudentId(), data.getSchoolId())) {
                            objects.add(data);
                        }
                    }
                }
            }
            //把原来 的 加到一起
            List<StatementData> objects1 = new ArrayList<>();
            for (StatementData data: statementDataIPage.getRecords()) {
                int isTo = 1;
                for (StatementData val: objects) {
                    if (Objects.equals(val.getSchoolId(), data.getSchoolId())) {
                        isTo = 2;
                    }
                }
                if (isTo == 1) {
                    objects1.add(data);
                }
            }
            if (objects1.size() > 0) {
                objects.addAll(objects1);
            }
            statementDataIPage.setRecords(objects);
        }
        return statementDataIPage;
    }
}
